import { initTableList } from '../mixins/listSettingData';

export const handelListSetting = (list, dynamicKeyListData, activeTabs) => {
  const dynamicKeyList = _.cloneDeep(dynamicKeyListData || {});
  let handoverDynamicKeyList = [];
  let journalKey = [];
  let costInfoKey = [];
  let moreJournalKey = [];
  Object.keys(dynamicKeyList).forEach(platformId => {
    handoverDynamicKeyList = handoverDynamicKeyList.concat(dynamicKeyList[platformId]['签约信息'] || []);
    journalKey = journalKey.concat(dynamicKeyList[platformId]['上刊价格'] || []);
    costInfoKey = costInfoKey.concat(dynamicKeyList[platformId]['成本信息'] || []);
    moreJournalKey = moreJournalKey.concat(dynamicKeyList[platformId]['更多上刊信息'] || []);
  });
  list = Array.isArray(list) ? list : list.tableList || [];
  list.forEach(i => {
    let list = [];
    if (i.fieldName === 'handoverKey') {
    // 签约信息
      list = handoverDynamicKeyList;
    } else if (i.fieldName === 'journalKey') {
      // 上刊价格
      list = journalKey;
    } else if (i.fieldName === 'costInfoKey') {
      // 成本信息
      list = costInfoKey;
    } else if (i.fieldName === 'moreJournalKey') {
      // 更多上刊信息
      list = moreJournalKey;
    }
    const initItem = initTableList.find(j => j.fieldName === i.fieldName);
    handelDiff(i, list, activeTabs, initItem);
  });
  return list;
};
const handelDiff = (column, dynamicKeyList, activeTabs, initItem = { children: [] }) => {
  // 从 自定义指标中删除已经不在最新的自定义字段列表中的字段
  const keys = dynamicKeyList.map(data => data.id);
  column.children = column.children.filter(field => {
    if (field.isDynamicKey && !keys.includes(Number(field.fieldName))) {
      return false;
    }
    return true;
  });

  // 从 dynamicKeyList 删除已经在自定义指标中的自定义字段
  dynamicKeyList = dynamicKeyList.filter(data => {
    const hasKey = column.children.some(field => field.fieldName.toString() === data.id.toString());
    return !hasKey;
  });
  // 把新增加的自定义字段 放入自定义指标配置中
  dynamicKeyList.forEach(data => {
    column.children.push({
      name: data.name,
      isShow: activeTabs !== 'all', // tab为"全部"时默认不勾选
      fieldName: data.id.toString(),
      isDynamicKey: true,
      modelKey: column.fieldName
    });
  });

  // 将新配的字段加到自定义配置当中
  const initList = initItem.children.filter(data => {
    const hasKey = column.children.some(field => field.fieldName.toString() === data.fieldName.toString());
    return !hasKey;
  });
  initList.forEach(data => {
    column.children.push({
      name: data.name,
      isShow: activeTabs !== 'all', // tab为"全部"时默认不勾选
      fieldName: data.fieldName.toString()
    });
  });

  // 将新增的属性加到历史数据中
  column.children.forEach(data => {
    const mapItem = initItem.children.find(i => i.fieldName === data.fieldName);
    if (mapItem) {
      const mapItemKeys = Object.keys(mapItem);
      mapItemKeys.forEach(key => {
        if (!data.hasOwnProperty(key)) {
          data[key] = mapItem[key];
        }
      });
    } else if (data.modelKey) { // 自定义字段，直接map对应编辑模块
      switch (data.modelKey) {
        case 'handoverKey':data.editModule = 'signInfoEdit'; break;
        case 'journalKey':data.editModule = 'journalEdit'; break;
        case 'moreJournalKey':data.editModule = 'journalEdit'; break;
        case 'costInfoKey':data.editModule = 'journalEdit'; break;
        case 'accountInfo':data.editModule = 'moreAccountInfoEdit'; break;
      }
    }
  });
};
